
export default class CollisionHelper {




    /**
     * 必须在同一层级中
     * @param other 
     * @param self 
     * @param func 
     */
    static collision(self: cc.Node, other: cc.Node): boolean {
        let rect1 = self.getBoundingBox()
        let rect2 = other.getBoundingBox();
        let flag = this.isRectCollideRect(rect1, rect2);
        return flag;
    }

    static isRectInRect(rect: cc.Rect, area: cc.Rect) {
        return rect.x >= area.x &&
            rect.x + rect.width <= area.x + area.width &&
            rect.y >= area.y &&
            rect.y + rect.height <= area.y + area.height;
    }

    static isRectCollideRect(r1: cc.Rect, r2: cc.Rect) {
        let flag = !(r2.x + r2.width < r1.x || r2.y > r1.y + r1.height || r2.x > r1.x + r1.width || r2.y + r2.height < r1.y);
        if (flag) {
        }
        return flag;
    }

    static isRectCollide(r1, r2) {

        let flag = !(r2.right < r1.left || r2.bottom > r1.top || r2.left > r1.right || r2.top < r1.bottom);

        return flag;
    }

    static isPointInRect2(point:cc.Node, rect:cc.Rect) {

        return point.x <= rect.x + rect.width && point.x >= rect.x && point.y <= rect.y + rect.height && point.y >= rect.y;
    }

    static isPointInRect(point: cc.Node, rx: number, ry: number, rw: number, rh: number): boolean {
        return point.x <= rx + rw && point.x >= rx && point.y <= ry + rh && point.y >= ry;
    }

    static isPositionInRect(point: cc.Vec2, rect: cc.Rect) {

        return point.x <= rect.x + rect.width && point.x >= rect.x && point.y <= rect.y + rect.height && point.y >= rect.y;
    }



    static isXYInRect(x, y, rect) {
        return x <= rect.right && x >= rect.left && y <= rect.top && y >= rect.bottom;
    }


    /**
     * 物体是否已经出屏
     * @param x 
     * @param y 
     * @param size 
     */
    static isOutScreen(x: number, y: number, w: number, h: number, dw: number, dh: number): boolean {
        // let screen = cc.view.getFrameSize();
        let hw = w / 2;
        let hh = h / 2;

        let flag = x < -hw || x > dw + hw || y > dh + hh || y < - hh;
        if (flag) {
            // cc.log('isOutScreen width ',dw,' height ',dh,' x ',x,' y ',y,' flag ',flag)
        }
        return flag
    }


    /**
     * 物体是否已经出屏
     * @param x 
     * @param y 
     * @param size 
     */
    static isOutScreenExceptTop(x: number, y: number, w: number, h: number, dw: number, dh: number): boolean {
        // let screen = cc.view.getFrameSize();
        let hw = w / 2;
        let hh = h / 2;

        let flag = x < - hw || x > dw + hw || y < - hh;
        if (flag) {
            // cc.log('isEnemyOutScreen width ',dw,' height ',dh,' x ',x,' y ',y,' flag ',flag)
        }
        //
        return flag
    }
}
